package org.jimmy.tang.innerclass;

/**
 * @author jimmy
 * 
 */
public class InnerclassDemo {

    public static void main(String... arg) {
        OuterClass outerClass = new OuterClass();
        outerClass.setSecret(100);
        System.out.println(outerClass.getInfo());
        System.out.println(outerClass.getInfo1());
        System.out.println(outerClass.getInfo2());
    }
}

class OuterClass {
    private int secret;

    public int getInfo() {
        Pair pair = new Pair();
        return pair.getSecret();
    }

    public int getInfo1() {
        Pair pair = this.new Pair(this);
        return pair.getSecret();
    }

    public int getInfo2() {
        @SuppressWarnings("hiding")
        class Pair {// 修饰语 only abstract or final is permitted
            public Pair() {
                take = secret;
            }

            public int getSecret() {
                return take;
            }

            private int take;
        }
        Pair pair = new Pair();
        return pair.getSecret();
    }

    public void setSecret(int secret) {
        this.secret = secret;
    }

    public class Pair {

        /**
         * 内部类总有一个隐式引用，它指向了创建它的外部类对象
         */
        public Pair() {
            take = secret;
        }

        /**
         * 
         * @param outerClass
         */
        public Pair(OuterClass outerClass) {
            OuterClass outer = outerClass;
            take = outer.secret;
        }

        public int getSecret() {
            return take;
        }

        private int take;
    }
}
